package com.tracesystem.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tracesystem.commons.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginErrorHandler implements AuthenticationFailureHandler {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public void onAuthenticationFailure(HttpServletRequest req, HttpServletResponse resp, AuthenticationException e) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf8");
        System.out.println("进入失败");
        Result result = Result.getInstance().setMsg(e.getMessage()).setCode(403).setData(null);
        resp.getWriter().write(objectMapper.writeValueAsString(result));
    }
}
